package com.adt.studioms.web.controller.user;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.asao.baseware.core.util.DateUtil;
import com.asao.baseware.core.web.controller.EntityCommand;
import com.asao.baseware.core.web.controller.EntityFormController;

public class TradeTxController extends EntityFormController<TradeTxCommand> {

    class TradeTxValidator implements Validator {

        public boolean supports(Class clazz) {
            return clazz.equals(TradeTxCommand.class);
        }

        public void validate(Object target, Errors errors) {
            TradeTxCommand cmd = (TradeTxCommand) target;

            if (cmd.inputDate() == null) {
                errors.rejectValue("inputDate", "tradeTx.inputDateIsInvalid", "Input date is invalid");
            }

            if (cmd.deliveryDate() == null) {
                errors.rejectValue("deliveryDate", "tradeTx.deliveryDateIsInvalid", "Delivery date is invalid");
            }
        }
    }

    protected void publishEntity(long id, TradeTxCommand command) {

    }

    protected void createEntity(TradeTxCommand command, BindException errors) {

    }

    protected void updateEntity(TradeTxCommand command, BindException errors) {

    }

    protected void deleteEntity(TradeTxCommand command, BindException errors) {

    }

}

class TradeTxDetailCommand {
    String taskCode;
    String photoSizeCode;
    String quantity;
    String price;
    String receipient;
    String deduction;
    String deductionReason;
    String errorMessage;

    public String getTaskCode() {
        return taskCode;
    }

    public void setTaskCode(String taskCode) {
        this.taskCode = taskCode;
    }

    public String getPhotoSizeCode() {
        return photoSizeCode;
    }

    public void setPhotoSizeCode(String photoSizeCode) {
        this.photoSizeCode = photoSizeCode;
    }

    public String getQuantity() {
        return quantity;
    }
    
    public Integer quantity() {
        return NumberUtils.isNumber(quantity) ? NumberUtils.toInt(quantity) : null;
    }

    public void setQuantity(String quantity) {
        this.quantity = quantity;
    }

    public String getPrice() {
        return price;
    }
    
    public Long price() {
        return NumberUtils.isNumber(price) ? NumberUtils.toLong(price) : null;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getReceipient() {
        return receipient;
    }

    public void setReceipient(String receipient) {
        this.receipient = receipient;
    }

    public String getDeduction() {
        return deduction;
    }

    public Long deduction() {
        return NumberUtils.isNumber(deduction) ? NumberUtils.toLong(deduction) : null;
    }

    public void setDeduction(String deduction) {
        this.deduction = deduction;
    }

    public String getDeductionReason() {
        return deductionReason;
    }

    public void setDeductionReason(String deductionReason) {
        this.deductionReason = deductionReason;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }
}

class TradeTxCommand extends EntityCommand {
    String customer;
    String inputDate;
    String deliveryDate;
    String[] taskCodes;
    String[] photoSizeCodes;
    String[] quantities;
    String[] prices;
    String[] receipients;
    String[] deductions;
    String[] deductionReasons;

    public TradeTxCommand() {
        inputDate = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
        deliveryDate = new SimpleDateFormat("dd/MM/yyyy").format(DateUtils.addDays(new Date(), -1));
    }

    public String getCustomer() {
        return customer;
    }

    public void setCustomer(String customer) {
        this.customer = customer;
    }

    public String getInputDate() {
        return inputDate;
    }

    public Long inputDate() {
        Date date = DateUtil.parseDate(inputDate);
        return date != null ? date.getTime() : null;
    }

    public void setInputDate(String inputDate) {
        this.inputDate = inputDate;
    }

    public String getDeliveryDate() {
        return deliveryDate;
    }

    public Long deliveryDate() {
        Date date = DateUtil.parseDate(deliveryDate);
        return date != null ? date.getTime() : null;
    }

    public void setDeliveryDate(String deliveryDate) {
        this.deliveryDate = deliveryDate;
    }

    public String[] getTaskCodes() {
        return taskCodes;
    }

    public void setTaskCodes(String[] taskCodes) {
        this.taskCodes = taskCodes;
    }

    public String[] getPhotoSizeCodes() {
        return photoSizeCodes;
    }

    public void setPhotoSizeCodes(String[] photoSizeCodes) {
        this.photoSizeCodes = photoSizeCodes;
    }

    public String[] getQuantities() {
        return quantities;
    }

    public void setQuantities(String[] quantities) {
        this.quantities = quantities;
    }

    public String[] getPrices() {
        return prices;
    }

    public void setPrices(String[] prices) {
        this.prices = prices;
    }

    public String[] getReceipients() {
        return receipients;
    }

    public void setReceipients(String[] receipients) {
        this.receipients = receipients;
    }

    public String[] getDeductions() {
        return deductions;
    }

    public void setDeductions(String[] deductions) {
        this.deductions = deductions;
    }

    public String[] getDeductionReasons() {
        return deductionReasons;
    }

    public void setDeductionReasons(String[] deductionReason) {
        this.deductionReasons = deductionReason;
    }
    
    public List<TradeTxDetailCommand> getDetails() {
        List<TradeTxDetailCommand> ret = new ArrayList<TradeTxDetailCommand>();
        if (taskCodes!=null) {
            for (int i=0; i<taskCodes.length; i++) {
                TradeTxDetailCommand ttxdc = new TradeTxDetailCommand();
                ttxdc.setTaskCode(taskCodes[i]);
                ttxdc.setPhotoSizeCode(photoSizeCodes[i]);
                ttxdc.setPrice(prices[i]);
                ttxdc.setQuantity(quantities[i]);
                ttxdc.setReceipient(receipients[i]);
                ttxdc.setDeduction(deductions[i]);
                ttxdc.setDeductionReason(deductionReasons[i]);
                ret.add(ttxdc);
            }
        }
        return ret;
    }
}